package com.portalEsporteClubeBahia.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.portalEsporteClubeBahia.app.ApplicationException;
import com.portalEsporteClubeBahia.app.bo.MatchBO;
import com.portalEsporteClubeBahia.app.vo.MatchVO;

public class FindAllMatchsNotFinishedAC extends HttpServlet {

	private static final long serialVersionUID = -2017693781775488449L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		JSONObject returning = new JSONObject();
		String status = "";
 		String message = "";
		JSONArray arrayData = new JSONArray();
		
		try {
			
			List<MatchVO> list = new MatchBO().findAllNotFinished();
			
			if(list!=null){
				for(int i=0;i<list.size();i++){
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("id", list.get(i).getId());
					jsonObject.put("championshipID", list.get(i).getChampionship().getId());
					jsonObject.put("championshipName", list.get(i).getChampionship().getName());
					jsonObject.put("stadiumID",list.get(i).getStadium().getId());
					jsonObject.put("stadimName", list.get(i).getStadium().getName());
					jsonObject.put("awayTeamId", list.get(i).getAwayTeam().getId());
					jsonObject.put("awayTeamName", list.get(i).getAwayTeam().getName());
					jsonObject.put("awayTeamShortName", list.get(i).getAwayTeam().getShortName());
					jsonObject.put("awayTeamUrlImage", list.get(i).getAwayTeam().getUrlImage());
					jsonObject.put("homeTeamID", list.get(i).getHomeTeam().getId());
					jsonObject.put("homeTeamName", list.get(i).getHomeTeam().getName());
					jsonObject.put("homeTeamShortName", list.get(i).getHomeTeam().getShortName());
					jsonObject.put("homeTeamUrlImage", list.get(i).getHomeTeam().getUrlImage());
					jsonObject.put("date", list.get(i).getDateFormat());
					jsonObject.put("dateUTC", list.get(i).getDateUTCFormat());
					jsonObject.put("homeGoal", list.get(i).getHomeGoals());
					jsonObject.put("awayGoal", list.get(i).getAwayGoals());
					jsonObject.put("finished", list.get(i).getFinished());
					jsonObject.put("active", list.get(i).getActive());
					arrayData.add(jsonObject);
				}
			}
			
			status =  "OK";
 			message = "";
			
		}catch (ApplicationException e) {
			status  =  "ERROR";
  			message = e.getMessage();
		}
		
		returning.put("status",status);
 		returning.put("message",message);
 		returning.put("data",arrayData);
		
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(returning.toString());
	}

}
